"""
Case Type   : 小型化
Case Name   : 端口号范围验证
Create At   : 2024/10/29
Owner       : ningyali
Description :
    1.获取安装包并解压
    2.安装时端口号范围验证
    3.清理数据环境
Expect      :
    1.成功
    2.成功
    3.清理完成
History     :
"""

import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.ComInstall import LiteInstall, LiteUnInstall
from testcase.utils.Logger import Logger


class Lite(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pkg_path = os.path.dirname(macro.DB_SCRIPT_PATH)
        self.com_install = LiteInstall(self.pkg_path, None, None)
        self.com_uninstall = LiteUnInstall(self.pkg_path)
        self.root_nodes = (Node('PrimaryRoot'),)
        self.user_nodes = (Node('PrimaryDbUser'),)
        self.user = None
        self.port = None
        self.assert_msg = f'the port number must be between 1024 and 65535'

    def test_upgrade(self):
        text = '----prestep1:获取可用用户和端口----'
        self.log.info(text)
        self.user, self.port = self.com_install.get_user_and_port(
            *self.root_nodes)
        self.log.info(f'self.user:{self.user}, self.port:{self.port}')
        self.user_nodes[0].ssh_user = self.user
        self.user_nodes[0].ssh_password = macro.COMMON_PASSWD

        self.data_path = os.path.join('/home', self.user, 'data')
        self.app_path = os.path.join('/home', self.user, 'app')
        self.com_install.data_path = self.data_path
        self.com_install.app_path = self.app_path

        text = '----prestep2:创建数据库用户和用户组;expect:创建成功----'
        self.log.info(text)
        self.com_install.prepare_user(self.user, *self.root_nodes)

        text = '----step1:获取数据库安装包并解压;expect:成功----'
        self.log.info(text)
        res = self.com_install.tar_pkg(self.root_nodes[0])
        self.assertIn('install.sh', res, f'执行失败: {text}')

        text = '----step2:端口号范围验证;expect:安装失败----'
        self.log.info(text)
        text = '----step2.1:端口号范围过大;expect:安装失败----'
        self.log.info(text)
        res = self.com_install.install_single(self.user_nodes[0], 655356)
        self.assertIn(self.assert_msg, res, f'执行失败: {text}')

        text = '----step2.2:执行卸载脚本;expect:成功----'
        self.log.info(text)
        res = self.com_uninstall.uninstall(*self.user_nodes)
        self.assertIn('success', res, f'执行失败: {text}')

        text = '----step2.3:端口号范围过小;expect:安装失败----'
        self.log.info(text)
        res = self.com_install.install_single(self.user_nodes[0], 1023)
        self.assertIn(self.assert_msg, res, f'执行失败: {text}')

    def tearDown(self):
        text = '----step4:清理环境;expect:成功----'
        self.log.info(text)
        res = self.com_uninstall.uninstall(*self.user_nodes)
        self.com_uninstall.clear_env(*self.root_nodes)
        self.assertIn('success', res, f'执行失败: {text}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
